package com.gdcp.config.security;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.stereotype.Component;

@Component("MyAuthenticationFailureHandler")
public class MyAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {

	@Override
	public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
			AuthenticationException exception) throws IOException {
		//Map<String,Object> object = new HashMap<>();
		JSONObject object = new JSONObject();
		object.put("success",false);
		object.put("message",exception.getMessage());
		//response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
		//response.getWriter().append(res.toString());
		// 失败返回json
		response.reset();
		response.setContentType("application/json;charset=UTF-8");
		response.getWriter().append(JSONObject.toJSONString(object));
	}
}
